//
//  PianKeViewController.m
//  花花时尚
//
//  Created by 王嘉彬 on 15/9/30.
//  Copyright © 2015年 ggs. All rights reserved.
//

#import "PianKeViewController.h"
#import "PianKeTableViewCell.h"
#import "NightMode.h"
#import "AsyncGETConnection.h"
#import "PianKeModel.h"
#import "Type1TableViewCell.h"
#import "Type2TableViewCell.h"

@interface PianKeViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)UISegmentedControl *segment;
@end

@implementation PianKeViewController
- (void)dealloc
{
    [_segment release];
    [_array release];
    [_tableView release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [self.view setNeedsDisplay];
    self.navigationController.navigationBar.barTintColor = [NightMode nightModeON]?[UIColor blackColor]:[UIColor whiteColor];
    UIImage * backImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NightMode nightModeON]?@"back":@"back_light" ofType:@"png"]];
    self.view.backgroundColor = [UIColor colorWithPatternImage:backImage];
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createAsy];
    self.navigationController.navigationBar.barTintColor = [NightMode nightModeON]?[UIColor blackColor]:[UIColor whiteColor];
    
    
    UIImage * backImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NightMode nightModeON]?@"back":@"back_light" ofType:@"png"]];
    self.view.backgroundColor = [UIColor colorWithPatternImage:backImage];
    
    NSArray * segArr = @[@"精选",@"碎片"];
    self.segment = [[UISegmentedControl alloc] initWithItems:segArr];
    _segment.selectedSegmentIndex = 0;
    _segment.frame = CGRectMake(0, 0, 140, 30);
    _segment.center = CGPointMake(self.view.frame.size.width/2, 34);
    
    [_segment addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    _segment.tintColor = [UIColor lightGrayColor];
    
    
    self.navigationItem.titleView = self.segment;
    
    [self createTableview];
}
- (void)segmentAction:(UISegmentedControl *)sender
{
    [self.tableView reloadData];
}

- (void)createTableview
{
    self.tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    [_tableView release];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    PianKeModel *model = [self.array objectAtIndex:indexPath.row];
    if ([model.type isEqualToNumber:[NSNumber numberWithInt:24]]) {
        static NSString *identifire = @"cell";
        PianKeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifire];
        if (cell == nil) {
            cell = [[PianKeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifire];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.nameLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
        cell.titleLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
        cell.contentLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
        cell.likeLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
        [cell setModel:model];
        cell.backgroundColor = [UIColor clearColor];
        return cell;
    } else if ([model.type isEqualToNumber:[NSNumber numberWithInt:1]]) {
        static NSString *iden = @"cell1";
        Type1TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
        if (cell == nil) {
            cell = [[Type1TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.nameLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
        cell.titleLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
        cell.contentLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
        cell.likeLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
        [cell setModel:model];
        cell.backgroundColor = [UIColor clearColor];
        
        return cell;
    } else if ([model.type isEqualToNumber:[NSNumber numberWithInt:2]]) {
        static NSString *iden = @"celltype2";
        Type2TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
        if (cell == nil) {
            cell = [[Type2TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.nameLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
        cell.titleLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
        cell.contentLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
        cell.likeLabel.textColor = [NightMode nightModeON]?[UIColor whiteColor]:[UIColor blackColor];
        [cell setModel:model];
        cell.backgroundColor = [UIColor clearColor];

        return cell;
    }
    
    else {
        static NSString *identifire = @"cell2";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifire];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifire];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        return cell;

    }
    
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 530;
}
- (void)createAsy
{
    [AsyncGETConnection asyncConnectionWithURLString:@"http://api2.pianke.me/pub/today" parmaters:nil block:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic = [dic objectForKey:@"data"];
        NSArray *arr = [dataDic objectForKey:@"list"];
        for (NSDictionary *mydic in arr) {
            PianKeModel *model = [[PianKeModel alloc] init];
            NSDictionary *user = [mydic objectForKey:@"userinfo"];
            [model setValuesForKeysWithDictionary:user];
            [model setValuesForKeysWithDictionary:mydic];
            [self.array addObject:model];
        }
        [self.tableView reloadData];
    }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
